// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    //数据列表
    listData: [],
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值

    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    this.getUsable();
    this.getNetData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoadMore) {
      this.getMoreData(this.data.page);
    } else {
      wx.showToast({
        title: "没有更多数据",
        icon: "none"
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData(this.data.page);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            usable: res.data.data.usable,
            love_name: res.data.data.love_name
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  initData() {
    this.page = 1;
    this.total_page = 0;
    this.goload = true;
    this.loading = true;
    this.allLoaded = false;
    this.isLoadMore = true;
    this.listData = [];
  },
  goDetailed(val) {
    console.log(val, '详情');

    wx.navigateTo({
      url: '/packageB/member/love/love_activation/love_activation' + '?id=' + val.currentTarget.dataset.id
    });
  },
  getNetData() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.activation-records.index");
    urlStr += "&page=" + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          this.setData({
            listData: res.data.data.data,
            total_page: res.data.data.last_page
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  getMoreData(page) {
    if (this.data.page == this.data.total_page) {
      app.tips("没有更多数据了");
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true,
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.activation-records.index");
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result == 1) {
            var myData = res.data.data;
            var listData = this.data.listData.concat(myData.data); //数组拼接
            this.setData({
              loading: false,
              allLoaded: false,
              listData
            });
          } else {
            var page = this.data.page - 1;
            this.setData({
              page,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
});